package com.tenement.controller;


import cn.hutool.json.JSONObject;
import com.mybatisflex.core.tenant.TenantManager;
import com.tenement.common.Axios;
import com.tenement.entity.User;
import com.tenement.entity.UserTenement;
import com.tenement.enums.RespBusiness;
import com.tenement.service.UserService;
import com.tenement.vo.QueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  控制层。
 *
 * @author BertXie
 * @since 1.0.1
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户管理接口")
@Validated
public class UserController {

    private final UserService userService;


    @ApiOperation(value = "操作用户的数据")
    @PutMapping("add")
    public Axios add(@RequestBody @Validated User user) {
        return Axios.success(userService.add(user));
    }


    @ApiOperation(value = "登录")
    @PutMapping("login")
    public Axios login(@RequestBody JSONObject jsonObject) {
        return Axios.success(userService.login(jsonObject.getStr("account"), jsonObject.getStr("password") ));
    }

    @ApiOperation(value = "微信授权验证登录")
    @GetMapping("miniLogin/{account}")
    public Axios miniLogin(@PathVariable(value = "account") String account) {
        return Axios.success(userService.miniLogin(account));
    }


    @ApiOperation(value = "封禁用户")
    @GetMapping("banUse/{account}")
    public Axios banUser(@PathVariable(value = "account") String account) {
        return userService.banUser(account) ? Axios.error(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }


    @ApiOperation("获取用户的列表")
    @PreAuthorize("@exp.hasAuthority('system:user:list')")
    @PostMapping("list")
    public Axios list(@RequestBody QueryVo queryVo) {
        return Axios.success(userService.list(queryVo));
    }

    @ApiOperation("获取员工的列表")
    @PostMapping("worker")
    public Axios worker(@RequestBody QueryVo queryVo) {
        return Axios.success(userService.worker(queryVo));
    }

    @ApiOperation("员工或者修改添加")
    @PutMapping("workerOperate")
    public Axios workerOperate(@RequestBody User user) {
        return Axios.success(userService.workerOperator(user));
    }

    @ApiOperation("员工删除")
    @DeleteMapping("removeWorker")
    public Axios removeWorker(@RequestBody List<Long> ids) {
        return Axios.success(userService.removeWorker(ids));
    }


    @ApiOperation("获取物业管理员")
    @GetMapping(value = {"adminBindSelect/{keyWord}","adminBindSelect"})
    public Axios adminBindSelect(@PathVariable(required = false) String keyWord) {
        return Axios.success(userService.adminBindSelect(keyWord));
    }

    @ApiOperation("设置物业管理员，并绑定租户")
    @PutMapping(value = "bindProperty")
    public Axios bindProperty(@RequestBody UserTenement userTenement) {
        return Axios.success(userService.bindProperty(userTenement));
    }

    @ApiOperation("退出登录")
    @GetMapping("loginOut")
    public Axios loginOut(HttpServletRequest req) {
        return userService.loginOut(req) ? Axios.error(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("添加住户")
    @PutMapping(value = {"residentOperate/{houseId}","residentOperate"})
    public Axios residentOperate(@PathVariable(required = false) Long houseId, @RequestBody User user) {
        return userService.residentOperate(houseId,user) > 0 ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("获取维修人员")
    @GetMapping(value = {"repairWorker/{keyWord}","repairWorker"})
    public Axios repairWorker(@PathVariable(required = false) String keyWord) {
        return Axios.success(userService.repairWorker(keyWord));
    }

    @ApiOperation("获取每人金句")
    @GetMapping("twins")
    public Axios twins() {
        return Axios.success(userService.twins());
    }

    @ApiOperation("获取首页的统计数据")
    @GetMapping("statistics/{userId}")
    public Axios statistics(@PathVariable Long userId) {
        return Axios.success(userService.statistics(userId));
    }

    @ApiOperation("获取用户根据id")
    @GetMapping("oneId/{id}")
    public Axios oneId(@PathVariable Long id) {
        User user = TenantManager.withoutTenantCondition(() -> userService.getById(id));
        return user != null ? Axios.success(user) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("修改个人信息")
    @PutMapping("updatePersonalInfo")
    public Axios updatePersonalInfo(@RequestBody User user) {
        return userService.updatePersonalInfo(user) ? Axios.success(true) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation("修改密码")
    @PutMapping("changePwd")
    public Axios changePwd(@RequestBody JSONObject req) {
        return userService.changePwd(req) ? Axios.success(true) : Axios.error(RespBusiness.FAIL);
    }

}
